package com.mm.domain;

import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Getter //自动生成Getter方法
@Setter //自动生成Setter方法
@Entity
@Table(name = "mmUser") //数据表名
public class User implements Serializable {
    @Id //主键
    @GeneratedValue(strategy = GenerationType.IDENTITY) //主键的生成策略
    private Long id;
    private String username;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")    //用于设置该字段时,自动进行格式化
    @Temporal(TemporalType.TIMESTAMP)   //从数据库中查询出来,"yyyy-MM-dd HH:mm:ss"这样格式的数据
    @Column(name = "create_time", columnDefinition = "timestamp default current_timestamp") //建字段时,默认该字段为当前时间
    private Date createTime;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)  //cascade为设置级联操作,fetch为设置加载操作
    @JoinTable(name = "mmUserNicknameJoin", //中间表名
            joinColumns = {@JoinColumn(name = "user_id")},      //中间表本表字段
            inverseJoinColumns = {@JoinColumn(name = "user_nickname_id")})  //中间表外键连接表字段
    @JsonBackReference  //序列化时忽略
    private Set<UserNickname> userNicknames = new HashSet<>();

    private String password;//密码
    private String roleNames;//用户所拥有的资源,逗号分隔
}
